package com.phb.puhuibao.common;

import java.io.IOException;

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;

public class HeaderFooter extends PdfPageEventHelper{
    public void onEndPage (PdfWriter writer, Document document) {
        Rectangle rect = writer.getBoxSize("art");
        
        
        Image img;
		try {
			img = Image.getInstance("D:/forword/brown.png");
	        img.scaleToFit(500,500);  
	        img.setAbsolutePosition((rect.getLeft() + rect.getRight()) / 2 -250 , rect.getTop() - 15);
	        img.setAlignment(Element.ALIGN_CENTER);          
	        writer.getDirectContent().addImage(img);
		} catch (  IOException | DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
//        
//        
//        switch(writer.getPageNumber() % 2) {
//        case 0:
//            ColumnText.showTextAligned(writer.getDirectContent(),
//                    Element.ALIGN_RIGHT, new Phrase("400-666-8618"),
//                    rect.getRight(), rect.getTop(), 0);
//            break;
//        case 1:
//            ColumnText.showTextAligned(writer.getDirectContent(),
//                    Element.ALIGN_LEFT, new Phrase("400-666-8618"),
//                    rect.getLeft(), rect.getTop(), 0);
//            break;
//        }
        ColumnText.showTextAligned(writer.getDirectContent(),
                Element.ALIGN_CENTER, new Phrase(String.format("%d", writer.getPageNumber())),
                (rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 40, 0);
    }
}